package code._17annotation._04handle;

import jdk.dynalink.beans.StaticClass;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Stack;

public class Ex {
    public static void main(String[] args) {
        User user = new User("zs", 20, "BJ");
        try {
            ValidateDataHandler.validate(user);
            System.out.println("user = " + user);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

//注解处理器
class ValidateDataHandler{
    //定义注解处理方法
    public static void validate(Object obj)throws IllegalAccessException{
        //获取字节码文件
        Class<?> aClass = obj.getClass();
        //获取成员变量对象
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field field : declaredFields) {
            //判断当前成员变量是否使用了注解
            boolean annotationPresent = field.isAnnotationPresent(ValidateData.class);
            if(annotationPresent){
                // 说明该成员变量使用了注解
                //获取成员变量对象的值
                Object value=field.get(obj);
                //获取成员变量的类型
                String fieldType = field.getType().getSimpleName();
                //获取注解实例
                ValidateData validateData = field.getAnnotation(ValidateData.class);
                if(value instanceof String){
                    checkStringLength(((String) value),validateData);
                }else{
                    checkIntLength(((int) value),validateData);
                }
            }
        }
    }

    private static void checkIntLength(int value, ValidateData validateData) {
        if (value < validateData.min() || value > validateData.max()) {
            // 抛出异常 报错
            throw new IllegalArgumentException(validateData.msg());
        }
    }

    private static void checkStringLength(String value, ValidateData validateData) {
        int max = validateData.max();
        int min = validateData.min();
        String msg = validateData.msg();
        if (value.length() < min || value.length() > max) {
            throw new IllegalArgumentException(msg);
        }
    }
}

// 使用元注解修饰
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
@interface ValidateData{
    // 最小长度
    int min() default 0;
    // 最大长度
    int max();

    // 报错信息
    String msg() default "";
}

class User{
    @ValidateData(min = 2, max = 5, msg = "名字长度不合法!")
    String name;
    @ValidateData(min = 1, max = 150, msg = "年龄不合法!")
    int age;
    String address;

    public User(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", address='" + address + '\'' +
                '}';
    }
}
